#include <Windows.h>
#include "Leg.h"
#include "Tree.h"
#include "Body.h"
#include "Neck.h"
#include "Face.h"
#include <glut.h>
#include "Butt.h"

using namespace std;

struct Perspective
{
	double lookX;
	double lookY;
	double lookZ;
};

int Sign = -1;
float Angle = 10;
int Axis = -1;
Perspective mainPers;
GLUquadricObj *p; /* pointer to quadric object */
Leg *l1;
Leg *l2;
Leg *l3;
Leg *l4;
Body *MainBody;
Neck *MainNeck;
Face *mainFace;
Butt *mainButt;

void DrawGiraffe(void)
{
	gluLookAt(0 + mainPers.lookX, 0 + mainPers.lookY, 10 + mainPers.lookZ, 0., 0., 0., 0., 10., 0.);	
	mainFace->DrawHip();
	MainNeck->DrawHip();
	MainBody->DrawHip();
	l4->DrawHip();
	l3->DrawHip();
	l1->DrawHip();
	l2->DrawHip();
	mainButt->DrawHip();
}
void display(void) 
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(0., 0., 17., 0., 0., 0., 0., 10., 0.);	
	MainBody->SetAngle(90, 0,1,0);
	MainNeck->SetAngle(90,1,0,0);
	mainFace->SetAngle(-90, 0,1,0);
	DrawGiraffe();
	glFlush();
	glutSwapBuffers();  
}
void change_viewer( unsigned char key, int x, int y )
{
	if (key == 'u')
	{
		mainPers.lookZ --;
	}

	if (key == 'i')
	{
		mainPers.lookZ ++;
	}

	if (key == 'j')
	{
		mainPers.lookX --;
	}

	if (key == 'k')
	{
		mainPers.lookX ++;
	}

	if(key == 'w')
	{
		
		if(Angle == 0)
		{
			Angle = 10;
		}
		else if (Angle == 10)
		{
			Angle = 1;
		}
		else if (Angle == 1)
		{
			Angle = -10;
		}
		else if (Angle == -10)
		{
			Angle = 0;
		}
		
		l1->SetAngle(Angle,0 ,1,0);
		l2->SetAngle(Angle, 0,1,0);
		l3->SetAngle(Angle*Sign, 0,1,0);
		l4->SetAngle(Angle*Sign,0 ,1,0);
		
		l4->SetLocation(-1,1,0,0);
		l3->SetLocation(-1,1,0,0);
		l2->SetLocation(-1,1,0,0);
		l1->SetLocation(-1,1,0,0);
		
		MainBody->SetLocation(-1,1,0,0);
		MainNeck->SetLocation(-1,1,0,0);
		mainFace->SetLocation(-1,1,0,0);
		mainButt->SetLocation(-1,1,0,0);
	}				
	if (key == 's')
	{
		if(Angle == 0)
		{
			Angle = 10;
		}
		else if (Angle == 10)
		{
			Angle = 1;
		}
		else if (Angle == 1)
		{
			Angle = -10;
		}
		else if (Angle == -10)
		{
			Angle = 0;
		}
		
		l1->SetAngle(Angle,0 ,1,0);
		l2->SetAngle(Angle, 0,1,0);
		l3->SetAngle(Angle*Sign, 0,1,0);
		l4->SetAngle(Angle*Sign,0 ,1,0);
		
		l4->SetLocation(1,1,0,0);
		l3->SetLocation(1,1,0,0);
		l2->SetLocation(1,1,0,0);
		l1->SetLocation(1,1,0,0);
		MainBody->SetLocation(1,1,0,0);
		MainNeck->SetLocation(1,1,0,0);
		mainFace->SetLocation(1,1,0,0);
		mainButt->SetLocation(1,1,0,0);
	}

	DrawGiraffe();
	glutPostRedisplay();

}
void myinit()
{
	mainPers.lookX = 0;
	mainPers.lookY = 0;
	mainPers.lookZ = 0;

	glViewport(0,0,800,600);
	glClearColor(1.0, 1.0, 1.0, 1.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	l1->SetAngle(90,1,0,0);
	l1->SetAngle(10,0,1,0);
	l2->SetAngle(90,1,0,0);
	l2->SetAngle(-10,0,1,0);
	l3->SetAngle(90,1,0,0);
	l3->SetAngle(-10,0,1,0);
	l4->SetAngle(90,1,0,0);
	l4->SetAngle(10,0,1,0);	
	MainBody->SetAngle(90, 0,1,0);
	MainNeck->SetAngle(90,1,0,0);
	mainFace->SetAngle(-90, 0,1,0);
	gluPerspective(60, 1, 1, 40);
	//glOrtho(-20.,20.,-20.,20.,-20.,20.);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void main(int argc, char **argv) 
{
	p=gluNewQuadric(); /* allocate quadric object */
	gluQuadricDrawStyle(p, GLU_LINE); /* render it as wireframe */
	
	l1 = new Leg(3,2,-2,p, 0.2, 0.4);
	l2 = new Leg(-3,2,-2,p, 0.2, 0.4);
	l3 = new Leg(-3,0,2,p,0.2, 0.4);
	l4 = new Leg(3,0,2,p,0.2, 0.4);
	MainBody = new Body(-4, 1, 0, p);
	MainNeck = new Neck(-4, 9, 0, p);
	mainFace = new Face(-4, 8, 0, p);
	mainButt = new Butt(4,1,0,p);
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutCreateWindow("3 Quadric Objects");
	myinit();
	glEnable(GL_DEPTH_TEST);
	glutKeyboardFunc( change_viewer );
	glutDisplayFunc(display);
	glutMainLoop();
}
